package mspbots.data.tl.prod.autotask.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.autotask.mapper.TaskAutotaskResourceMapper;
import mspbots.data.tl.prod.autotask.model.TaskAutotaskResource;
import mspbots.data.tl.prod.autotask.service.TaskAutotaskResourceService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Service
@Log4j2
@RequiredArgsConstructor
@DS("master")
public class TaskAutotaskResourceServiceImpl extends AbstractBaseServiceImpl<TaskAutotaskResourceMapper, TaskAutotaskResource>
        implements TaskAutotaskResourceService {

    @Override
    public void sync(ResultEntity content) {
        log.debug("Auto task phase receiver content {}", content);
        JsonNode entity = content.getEntity();
        TaskAutotaskResource data = TaskAutotaskResource.builder()
                .resourceId(super.getLongValue(entity.get("id")))
                .defaultServiceDeskRoleId(ObjectUtils.isEmpty(entity.get("defaultServiceDeskRoleID")) ? null : entity.get("defaultServiceDeskRoleID").asLong())
                .accountingReferenceId(ObjectUtils.isEmpty(entity.get("accountingReferenceID")) ? null : entity.get("accountingReferenceID").asText())
                .dateFormat(ObjectUtils.isEmpty(entity.get("dateFormat")) ? null : entity.get("dateFormat").asText())
                .email(ObjectUtils.isEmpty(entity.get("email")) ? null : entity.get("email").asText())
                .email2(ObjectUtils.isEmpty(entity.get("email2")) ? null : entity.get("email2").asText())
                .email3(ObjectUtils.isEmpty(entity.get("email3")) ? null : entity.get("email3").asText())
                .emailTypeCode(ObjectUtils.isEmpty(entity.get("emailTypeCode")) ? null : entity.get("emailTypeCode").asText())
                .emailTypeCode2(ObjectUtils.isEmpty(entity.get("emailTypeCode2")) ? null : entity.get("emailTypeCode2").asText())
                .emailTypeCode3(ObjectUtils.isEmpty(entity.get("emailTypeCode3")) ? null : entity.get("emailTypeCode3").asText())
                .firstName(ObjectUtils.isEmpty(entity.get("firstName")) ? null : entity.get("firstName").asText())
                .gender(ObjectUtils.isEmpty(entity.get("gender")) ? null : entity.get("gender").asText())
                .homePhone(ObjectUtils.isEmpty(entity.get("homePhone")) ? null : entity.get("homePhone").asText())
                .initials(ObjectUtils.isEmpty(entity.get("initials")) ? null : entity.get("initials").asText())
                .lastName(ObjectUtils.isEmpty(entity.get("lastName")) ? null : entity.get("lastName").asText())
                .middleName(ObjectUtils.isEmpty(entity.get("middleName")) ? null : entity.get("middleName").asText())
                .mobilePhone(ObjectUtils.isEmpty(entity.get("mobilePhone")) ? null : entity.get("mobilePhone").asText())
                .numberFormat(ObjectUtils.isEmpty(entity.get("numberFormat")) ? null : entity.get("numberFormat").asText())
                .officeExtension(ObjectUtils.isEmpty(entity.get("officeExtension")) ? null : entity.get("officeExtension").asText())
                .officePhone(ObjectUtils.isEmpty(entity.get("officePhone")) ? null : entity.get("officePhone").asText())
                .resourceType(ObjectUtils.isEmpty(entity.get("resourceType")) ? null : entity.get("resourceType").asText())
                .timeFormat(ObjectUtils.isEmpty(entity.get("timeFormat")) ? null : entity.get("timeFormat").asText())
                .title(ObjectUtils.isEmpty(entity.get("title")) ? null : entity.get("title").asText())
                .travelAvailabilityPct(ObjectUtils.isEmpty(entity.get("travelAvailabilityPct")) ? null : entity.get("travelAvailabilityPct").asText())
                .userName(ObjectUtils.isEmpty(entity.get("userName")) ? null : entity.get("userName").asText())
                .greeting(ObjectUtils.isEmpty(entity.get("greeting")) ? null : entity.get("greeting").asInt())
                .licenseType(ObjectUtils.isEmpty(entity.get("licenseType")) ? null : entity.get("licenseType").asInt())
                .locationId(ObjectUtils.isEmpty(entity.get("locationID")) ? null : entity.get("locationID").asInt())
                .payrollType(ObjectUtils.isEmpty(entity.get("payrollType")) ? null : entity.get("payrollType").asInt())
                .suffix(ObjectUtils.isEmpty(entity.get("suffix")) ? null : entity.get("suffix").asInt())
                .userType(ObjectUtils.isEmpty(entity.get("userType")) ? null : entity.get("userType").asInt())
                .internalCost(ObjectUtils.isEmpty(entity.get("internalCost")) ? null : entity.get("internalCost").asDouble())
                .surveyResourceRating(ObjectUtils.isEmpty(entity.get("surveyResourceRating")) ? null : entity.get("surveyResourceRating").asDouble())
                .isActive(ObjectUtils.isEmpty(entity.get("isActive")) ? null : entity.get("isActive").asBoolean())
                .hireDate(ObjectUtils.isEmpty(entity.get("hireDate")) || "null".equalsIgnoreCase(entity.get("hireDate").asText()) ? null: LocalDateTime.parse(entity.get("hireDate").textValue(), DateTimeFormatter.ISO_OFFSET_DATE_TIME))
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(content.getTenantId());

        TaskAutotaskResource dbData = super.baseMapper.selectOne(Wrappers.<TaskAutotaskResource>lambdaQuery()
                .eq(TaskAutotaskResource::getTenantCode, data.getTenantCode())
                .eq(TaskAutotaskResource::getResourceId, data.getResourceId())
                .last("limit 1"));
        if (ObjectUtils.isEmpty(dbData)) {
            super.baseMapper.insert(data);
        } else {
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }
}
